package com.ruoyi.appraise.service.impl;

import com.ruoyi.appraise.domain.UserAppraise;
import com.ruoyi.appraise.mapper.UserAppraiseMapper;
import com.ruoyi.appraise.service.IUserAppraiseService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.order.domain.TechnicianItemOrder;
import com.ruoyi.order.mapper.TechnicianItemOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户评价Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@Service
public class UserAppraiseServiceImpl implements IUserAppraiseService {
    @Autowired
    private UserAppraiseMapper userAppraiseMapper;
    @Resource
    private TechnicianItemOrderMapper technicianItemOrderMapper;

    /**
     * 查询用户评价
     *
     * @param id 用户评价主键
     * @return 用户评价
     */
    @Override
    public UserAppraise selectUserAppraiseById(Long id) {
        return userAppraiseMapper.selectUserAppraiseById(id);
    }

    /**
     * 查询用户评价列表
     *
     * @param userAppraise 用户评价
     * @return 用户评价
     */
    @Override
    public List<UserAppraise> selectUserAppraiseList(UserAppraise userAppraise) {
        return userAppraiseMapper.selectUserAppraiseList(userAppraise);
    }

    /**
     * 新增用户评价
     *
     * @param userAppraise 用户评价
     * @return 结果
     */
    @Override
    public int insertUserAppraise(UserAppraise userAppraise) {
        TechnicianItemOrder technicianItemOrder = technicianItemOrderMapper.selectOne(userAppraise.getOrderNo());
        if (CheckEmptyUtil.isEmpty(technicianItemOrder)) {
            throw new RuntimeException("请输入有效订单号");
        }
        userAppraise.setUserId(technicianItemOrder.getUserId());
        userAppraise.setTid(technicianItemOrder.getTid());
        userAppraise.setItemId(technicianItemOrder.getItemId());
        userAppraise.setItemName(technicianItemOrder.getItemName());
        userAppraise.setItemPicture(technicianItemOrder.getItemPicture());
        userAppraise.setItemTime(technicianItemOrder.getItemTime());
        userAppraise.setItemCount(technicianItemOrder.getOrderNum());
        userAppraise.setItemPrice(technicianItemOrder.getOrderItemPrice());
        userAppraise.setCreateTime(DateUtils.getNowDate());
        return userAppraiseMapper.insertUserAppraise(userAppraise);
    }

    /**
     * 修改用户评价
     *
     * @param userAppraise 用户评价
     * @return 结果
     */
    @Override
    public int updateUserAppraise(UserAppraise userAppraise) {
        userAppraise.setUpdateTime(DateUtils.getNowDate());
        return userAppraiseMapper.updateUserAppraise(userAppraise);
    }

    /**
     * 批量删除用户评价
     *
     * @param ids 需要删除的用户评价主键
     * @return 结果
     */
    @Override
    public int deleteUserAppraiseByIds(Long[] ids) {
        return userAppraiseMapper.deleteUserAppraiseByIds(ids);
    }

    /**
     * 删除用户评价信息
     *
     * @param id 用户评价主键
     * @return 结果
     */
    @Override
    public int deleteUserAppraiseById(Long id) {
        return userAppraiseMapper.deleteUserAppraiseById(id);
    }
}
